package WordCount;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

public class File {
	public ArrayList<String> readfile(String file){
		ArrayList<String> arrylist=new ArrayList<>();
		try{
			FileReader fr=new FileReader(file);
			BufferedReader br=new BufferedReader(fr);
			String str;
			while((str=br.readLine())!=null){
				arrylist.add(str);
			}
			fr.close();
			br.close();
		}catch (IOException e){
			e.printStackTrace();
		}
		return arrylist;
	}
	
	
	
	public void writefile(String path,int CountChars,int CountWords,int CountLine,ArrayList<Entry<String, Integer>> wordtop){
		try{
			FileWriter fw=new FileWriter(path);
			BufferedWriter bw=new BufferedWriter(fw);
			String str1="characters:"+CountChars;
			String str2="words:"+CountWords;
			String str3="lines:"+CountLine;
			bw.write(str1);
			bw.newLine();
			bw.write(str2);
			bw.newLine();
			bw.write(str3);
			bw.newLine();
			for(int i=0;i<wordtop.size();i++){
				Map.Entry<String,Integer> e=wordtop.get(i);
				bw.write(e.getKey()+ ":" +e.getValue());
				bw.newLine();
			}
			bw.close();
			fw.close();
		}catch (IOException e){
			e.printStackTrace();
		}
	}

}
